﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Jungle.Code.Core;
using System.Collections.Generic;
using Jungle.Code.Utils;

namespace Jungle.Admin.Controls
{
    public partial class Structure : System.Web.UI.UserControl, IPostBackEventHandler
	{
		private int currentNodeID = 0;
        private string currentLangID = "1";
		protected void Page_Load(object sender, EventArgs e) {
			if (Request["nodeID"] != null)
				currentNodeID = Int32.Parse(Request["nodeID"]);
            BuildStructureTree();
		}
        //--------------------------------------------------------------------
        protected override void OnInit(EventArgs e)
        {
            LoadLangs();
            base.OnInit(e);
        }
        //--------------------------------------------------------------------
		private void LoadLangs() {

            ClientScriptManager cs = Page.ClientScript;
            PostBackOptions opt = new PostBackOptions(this);
            opt.ActionUrl = Context.Request.RawUrl;

            HttpCookie cookie = Request.Cookies["CurrLang"];
            if (cookie != null)
            {
                currentLangID = cookie["lang"];
            }

			List<Language> langs = Language.GetAllLanguages();
            foreach (Language l in langs)
            {
                Label lb = new Label();
                lb.Text = "&nbsp;&nbsp;" + l.Name + "&nbsp;&nbsp;";
                opt.Argument = "chL," + l.LangID.ToString();
                lb.Attributes.Add("onclick", cs.GetPostBackEventReference(opt));
                lb.Attributes.Add("style", "cursor: pointer");
                if (l.LangID.ToString() == currentLangID) lb.Font.Bold = true;

                plh_Languages.Controls.Add(lb);
            }
		}
        //--------------------------------------------------------------------
        public void RaisePostBackEvent(string eventArgument)
        {
            int pos = eventArgument.IndexOf(',');
            int l = eventArgument.Length;
            string s1 = eventArgument.Substring(0, pos);
            string s2 = eventArgument.Substring(pos + 1, (l - (pos + 1)));

            if (s1 == "chL") // измнение языка
            {
                HttpCookie cookie = Response.Cookies["CurrLang"];
                cookie["lang"] = s2;
                currentLangID = s2;

                Node topNode = Node.GetTopByLangID(Convert.ToInt32(currentLangID));
                Response.Redirect("Default.aspx?nodeID=" + topNode.NodeID.ToString() + "&cmd=cont");
            }
        }
        //--------------------------------------------------------------------
		private void BuildStructureTree() {
            Node topNode = Node.GetTopByLangID(Convert.ToInt32(currentLangID));

			HtmlGenericControl control1 = new HtmlGenericControl();
			HtmlGenericControl control2 = new HtmlGenericControl("ul");
			HyperLink link1 = new HyperLink();
			link1.Text = topNode.Name;
            link1.NavigateUrl = UrlHelper.GetAdminUrl() + "Default.aspx?nodeID=" + topNode.NodeID + "&cmd=cont";
			if (topNode.NodeID == currentNodeID)
				link1.CssClass = "activ";
			else
				link1.CssClass = "noactiv";
			HyperLink child = new HyperLink();
            Image add = new Image();
            add.ImageUrl = "~/Admin/img/item.jpg";
            add.Width = 11;
            add.Height = 12;
            child.Controls.Add(add);
			child.NavigateUrl = UrlHelper.GetAdminUrl() + "Default.aspx?nodeID=" + topNode.NodeID + "&cmd=add";
			control2.Controls.Add(child);
			control2.Controls.Add(link1);
			control2.Controls.Add(this.BuildItemTree(topNode.NodeID));
			control1.Controls.Add(control2);

			structureTree.Controls.Add(control1);


		}
        //--------------------------------------------------------------------
		private HtmlGenericControl BuildItemTree(int parentNodeID) {
			List<Node> nodes = new List<Node>();
			nodes = Node.GetByParentNodeID(parentNodeID);

			HtmlGenericControl control1 = new HtmlGenericControl();
			foreach (Node node in nodes) {
				HtmlGenericControl control2 = new HtmlGenericControl("ul");
				HyperLink link1 = new HyperLink();
				link1.Text = node.Name;
				link1.NavigateUrl = UrlHelper.GetAdminUrl() + "Default.aspx?nodeID=" + node.NodeID + "&cmd=cont";
				if (node.NodeID== currentNodeID)
					link1.CssClass = "activ";
				else
					link1.CssClass = "noactiv";
				HyperLink child = new HyperLink();
                Image unvisible = new Image();
                unvisible.ImageUrl = "~/Admin/img/unvisible.jpg";
                Image add = new Image();
                add.Width = 11;
                add.Height = 12;
                add.ImageUrl = "~/Admin/img/item.jpg";
                child.Controls.Add(add);
				child.NavigateUrl = UrlHelper.GetAdminUrl() + "Default.aspx?nodeID=" + node.NodeID + "&cmd=add";
				control2.Controls.Add(child);
				control2.Controls.Add(link1);
                if (!node.Enabled) control2.Controls.Add(unvisible);
                control2.Controls.Add(this.BuildItemTree(node.NodeID));
				control1.Controls.Add(control2);

			}

			return control1;
		}
	}
}